Getting Started topic

The primary API for the working with a tree state machine is provided by the tree_state_machine library. A relatively simple API for defining state trees is provided by the delegate_builders library, though extension points are provided in the build library for creating more sophisticated ones.

A typical usage looks like the following:

import 'package:tree_state_machine/delegate_builders.dart';
import 'package:tree_state_machine/tree_state_machine.dart';


// Define state keys that identify the states in the state tree
sealed class States {
   static const state1 = StateKey('state1');
   static const state1 = StateKey('state2');
}

// Define the state tree
var stateTree = StateTree(
   InitialChild(States.state1), 
   childStates: [
      State(
         States.state1, 
         onMessage: (MessageContext ctx) => ctx.message == 'go'
            ? ctx.goTo(States.state2) 
            : ctx.unhandled(),
      ),
      State(States.state2),
   ],
);

// Create and start a state machine for the state tree
var machine = TreeStateMachine(stateTree);
var currentState = await machine.start();

// Send a message to be processed by the current state, which may potentially
// cause a transition to a different state
await currentState.post('go');

Classes

DataStateKey<D> Getting Started
An identifier for a data state, carrying state data of type D, within a tree state machine.
StateKey Getting Started
An identifier for a state within a tree state machine.
StateTree Getting Started State Trees
Defines a state tree that can be used in conjunction with a TreeStateMachine.
TreeStateMachine Getting Started State Machines
A state machine that manages transitions among the states in a state tree.